﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CodeGenerator
{
    class SymbolTableStack
    {
        private Stack<SymbolTable> stack;
        public int CurrentNestingLevel { get; protected set; }

        public SymbolTableStack()
        {
            stack = new Stack<SymbolTable>();
            stack.Push(new SymbolTable(0));
        }

        public SymbolTable GetLocalSymbolTable()
        {
            return stack.Peek();
        }

        public Symbol EnterLocal(String name)
        {
            return GetLocalSymbolTable().EnterSymbol(name);
        }

        public Symbol LookupLocal(String name)
        {
            return GetLocalSymbolTable().Lookup(name);
        }

        public Symbol Lookup(String name)
        {
            Symbol result = null;

            foreach (SymbolTable t in stack)
            {
                Symbol localSymbol = t.Lookup(name);
                result = (localSymbol != null) ? localSymbol : result;
            }

            return result;
        }
    }
}
